Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

157
Visualizações
map[complement] === undefined vs !map[complement]

I am solving the following leetcode question and have the solution below

const twoSum = (numbers, target) => {
    let map = {}
    let result = []
    for (let i = 0; i < numbers.length; i++) {
        let complement = target - numbers[i]
        if (map[complement] === undefined) {
            map[numbers[i]] = i
        } else {
            result[0] = map[complement] + 1
            result[1] = i + 1
        }
    }
    return result
};

If I replace map[complement] === undefined with !map[complement] I return an empty array. In my mind both should return true. Why does the latter breaks my code?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

map[complement] === undefined only becomes true when there is no element with the key equal to complement while !map[complement] becomes true in all the cases where its result is a falsy value.

Falsy values include but are not limited to, "", false, undefined, null, 0, -0, etc.

In other words, the first case is a sub set of the second.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda